package com.doordu.soa.service.web.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.Resource;

public class FileOperatorUtils {

	/**
	 * 新建文件
	 * 
	 * @param path
	 *            文件存放路径
	 * @param bytes
	 *            文件内容
	 * @return
	 * @throws IOException
	 */
	public static boolean newFile(String path, InputStream ins) throws IOException {

		int bytesum = 0;
		int byteread = 0;
		File file = new File(path);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		OutputStream out = null;
		try {
			out = new BufferedOutputStream(new FileOutputStream(file));
			byte[] buffer = new byte[1024];
			while ((byteread = ins.read(buffer)) != -1) {
				bytesum += byteread;
				out.write(buffer, 0, byteread);
				// out.write(buffer);
			}
			out.flush();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (out != null)
				out.close();
		}
	}

	public static boolean write(String path, OutputStream out) throws IOException {
		InputStream in = null;
		OutputStream bufferOut = null;
		try {
			File localFile = new File(path);
			in = new BufferedInputStream(new FileInputStream(localFile));
			bufferOut = new BufferedOutputStream(out);
			byte[] buffer = new byte[1024];
			while (in.read(buffer) != -1) {
				bufferOut.write(buffer);
			}
			bufferOut.flush();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (in != null)
				in.close();
		}
	}

	public static void download(HttpServletResponse response, Resource resource) throws IOException {
		File file = resource.getFile();
		String filename = file.getName();// 获取日志文件名称
		InputStream fis = resource.getInputStream();
		byte[] buffer = new byte[fis.available()];
		fis.read(buffer);
		fis.close();
		response.reset();
		// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
		response.addHeader("Content-Disposition",
				"attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
		response.addHeader("Content-Length", "" + file.length());
		OutputStream os = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("application/octet-stream");
		os.write(buffer);// 输出文件
		os.flush();
		os.close();
	}

	/**
	 * 新建文件
	 * 
	 * @param path
	 *            文件存放路径
	 * @param bytes
	 *            文件内容
	 * @return
	 */
	public static boolean newFile(String path, byte[] bytes) {

		File file = new File(path);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(bytes);
			fos.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			try {
				fos.close();
			} catch (IOException ioe1) {
				e.printStackTrace();
			}
			file.delete();
			return false;
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 移动文件
	 * 
	 * @param oldFilePath
	 *            移动前的文件路径
	 * @param newFilePath
	 *            移动后的文件路径
	 * @return
	 */
	public static boolean moveFile(String oldFilePath, String newFilePath) {
		// TODO 未完成
		return false;
	}

	/**
	 * 删除文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @return
	 */
	public static boolean delFile(String filePath) {
		// TODO 未完成
		return false;
	}
}
